<template>
  <div class="dashboard">
    <!-- 欢迎卡片 -->
    <el-card class="welcome-card" shadow="never">
      <div class="welcome-content">
        <div class="welcome-text">
          <h2>欢迎回来，{{ authStore.userInfo?.name || '用户' }}！</h2>
          <p>今天是{{ currentDate }}，祝您工作愉快</p>
        </div>
        <div class="welcome-avatar">
          <el-avatar :size="60" :src="authStore.userInfo?.avatar">
            <el-icon size="30"><UserFilled /></el-icon>
          </el-avatar>
        </div>
      </div>
    </el-card>
    
    <!-- 数据统计卡片 -->
    <el-row :gutter="20" class="stats-row">
      <el-col :xs="12" :sm="6" :md="6" :lg="6" :xl="6">
        <el-card class="stats-card" shadow="hover">
          <div class="stats-item">
            <div class="stats-icon customer">
              <el-icon size="24"><User /></el-icon>
            </div>
            <div class="stats-content">
              <div class="stats-value">{{ stats.totalCustomers }}</div>
              <div class="stats-label">总客户数</div>
            </div>
          </div>
        </el-card>
      </el-col>
      
      <el-col :xs="12" :sm="6" :md="6" :lg="6" :xl="6">
        <el-card class="stats-card" shadow="hover">
          <div class="stats-item">
            <div class="stats-icon activity">
              <el-icon size="24"><Calendar /></el-icon>
            </div>
            <div class="stats-content">
              <div class="stats-value">{{ stats.totalActivities }}</div>
              <div class="stats-label">活动数量</div>
            </div>
          </div>
        </el-card>
      </el-col>
      
      <el-col :xs="12" :sm="6" :md="6" :lg="6" :xl="6">
        <el-card class="stats-card" shadow="hover">
          <div class="stats-item">
            <div class="stats-icon tour">
              <el-icon size="24"><MapLocation /></el-icon>
            </div>
            <div class="stats-content">
              <div class="stats-value">{{ stats.totalTours }}</div>
              <div class="stats-label">出团数量</div>
            </div>
          </div>
        </el-card>
      </el-col>
      
      <el-col :xs="12" :sm="6" :md="6" :lg="6" :xl="6">
        <el-card class="stats-card" shadow="hover">
          <div class="stats-item">
            <div class="stats-icon revenue">
              <el-icon size="24"><Money /></el-icon>
            </div>
            <div class="stats-content">
              <div class="stats-value">¥{{ stats.totalRevenue }}</div>
              <div class="stats-label">总收入</div>
            </div>
          </div>
        </el-card>
      </el-col>
    </el-row>
    
    <!-- 图表和列表区域 -->
    <el-row :gutter="20" class="content-row">
      <!-- 趋势图表 -->
      <el-col :xs="24" :sm="24" :md="16" :lg="16" :xl="16">
        <el-card title="业务趋势" shadow="never">
          <template #header>
            <div class="card-header">
              <span>业务趋势</span>
              <el-button-group>
                <el-button :type="chartPeriod === '7d' ? 'primary' : ''" @click="chartPeriod = '7d'">
                  7天
                </el-button>
                <el-button :type="chartPeriod === '30d' ? 'primary' : ''" @click="chartPeriod = '30d'">
                  30天
                </el-button>
                <el-button :type="chartPeriod === '90d' ? 'primary' : ''" @click="chartPeriod = '90d'">
                  90天
                </el-button>
              </el-button-group>
            </div>
          </template>
          
          <div class="chart-container">
            <div class="chart-placeholder">
              <el-icon size="64" color="#ddd"><TrendCharts /></el-icon>
              <p>图表数据加载中...</p>
            </div>
          </div>
        </el-card>
      </el-col>
      
      <!-- 快速操作和最新动态 -->
      <el-col :xs="24" :sm="24" :md="8" :lg="8" :xl="8">
        <!-- 快速操作 -->
        <el-card title="快速操作" shadow="never" class="quick-actions">
          <div class="action-grid">
            <div class="action-item" @click="router.push('/customers/create')">
              <el-icon size="20"><Plus /></el-icon>
              <span>新增客户</span>
            </div>
            <div class="action-item" @click="router.push('/activities')">
              <el-icon size="20"><Calendar /></el-icon>
              <span>创建活动</span>
            </div>
            <div class="action-item" @click="router.push('/tours')">
              <el-icon size="20"><MapLocation /></el-icon>
              <span>安排出团</span>
            </div>
            <div class="action-item" @click="router.push('/statistics')">
              <el-icon size="20"><DataAnalysis /></el-icon>
              <span>查看报表</span>
            </div>
          </div>
        </el-card>
        
        <!-- 最新动态 -->
        <el-card title="最新动态" shadow="never" class="recent-activities">
          <div class="activity-list">
            <div
              v-for="activity in recentActivities"
              :key="activity.id"
              class="activity-item"
            >
              <div class="activity-icon">
                <el-icon :color="activity.color"><component :is="activity.icon" /></el-icon>
              </div>
              <div class="activity-content">
                <div class="activity-text">{{ activity.text }}</div>
                <div class="activity-time">{{ activity.time }}</div>
              </div>
            </div>
          </div>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { useAuthStore } from '@/stores/auth'
import dayjs from 'dayjs'

const router = useRouter()
const authStore = useAuthStore()

// 当前日期
const currentDate = ref(dayjs().format('YYYY年MM月DD日'))

// 图表时间段
const chartPeriod = ref('7d')

// 统计数据
const stats = reactive({
  totalCustomers: 1286,
  totalActivities: 42,
  totalTours: 18,
  totalRevenue: '2,450,000'
})

// 最新动态
const recentActivities = ref([
  {
    id: 1,
    icon: 'User',
    color: '#409eff',
    text: '新增客户：张小明',
    time: '2分钟前'
  },
  {
    id: 2,
    icon: 'Calendar',
    color: '#67c23a',
    text: '活动报名：春季赏花游',
    time: '15分钟前'
  },
  {
    id: 3,
    icon: 'MapLocation',
    color: '#e6a23c',
    text: '出团确认：三亚5日游',
    time: '1小时前'
  },
  {
    id: 4,
    icon: 'Money',
    color: '#f56c6c',
    text: '收款确认：¥12,800',
    time: '2小时前'
  }
])

// 组件挂载时
onMounted(() => {
  // TODO: 加载统计数据
  loadStats()
  // TODO: 加载最新动态
  loadRecentActivities()
})

// 加载统计数据
const loadStats = async () => {
  try {
    // TODO: 调用API获取统计数据
    console.log('加载统计数据')
  } catch (error) {
    console.error('加载统计数据失败:', error)
  }
}

// 加载最新动态
const loadRecentActivities = async () => {
  try {
    // TODO: 调用API获取最新动态
    console.log('加载最新动态')
  } catch (error) {
    console.error('加载最新动态失败:', error)
  }
}
</script>

<style scoped>
.dashboard {
  padding: 0;
}

.welcome-card {
  margin-bottom: 20px;
  border: none;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
}

.welcome-card :deep(.el-card__body) {
  padding: 30px;
}

.welcome-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.welcome-text h2 {
  margin: 0 0 8px 0;
  font-size: 24px;
  font-weight: 600;
}

.welcome-text p {
  margin: 0;
  font-size: 14px;
  opacity: 0.9;
}

.stats-row {
  margin-bottom: 20px;
}

.stats-card {
  border: none;
  transition: transform 0.3s, box-shadow 0.3s;
}

.stats-card:hover {
  transform: translateY(-4px);
}

.stats-item {
  display: flex;
  align-items: center;
  padding: 10px 0;
}

.stats-icon {
  width: 50px;
  height: 50px;
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 15px;
  color: white;
}

.stats-icon.customer {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}

.stats-icon.activity {
  background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
}

.stats-icon.tour {
  background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
}

.stats-icon.revenue {
  background: linear-gradient(135deg, #43e97b 0%, #38f9d7 100%);
}

.stats-content {
  flex: 1;
}

.stats-value {
  font-size: 24px;
  font-weight: 600;
  color: #333;
  margin-bottom: 4px;
}

.stats-label {
  font-size: 14px;
  color: #666;
}

.content-row {
  gap: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.chart-container {
  height: 300px;
  position: relative;
}

.chart-placeholder {
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: #999;
}

.chart-placeholder p {
  margin-top: 15px;
  font-size: 14px;
}

.quick-actions {
  margin-bottom: 20px;
}

.action-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 15px;
}

.action-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 20px;
  border: 1px solid #e4e7ed;
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.3s;
}

.action-item:hover {
  border-color: #409eff;
  color: #409eff;
  transform: translateY(-2px);
}

.action-item span {
  margin-top: 8px;
  font-size: 14px;
}

.activity-list {
  max-height: 240px;
  overflow-y: auto;
}

.activity-item {
  display: flex;
  align-items: flex-start;
  padding: 12px 0;
  border-bottom: 1px solid #f0f0f0;
}

.activity-item:last-child {
  border-bottom: none;
}

.activity-icon {
  margin-right: 12px;
  margin-top: 2px;
}

.activity-content {
  flex: 1;
}

.activity-text {
  font-size: 14px;
  color: #333;
  margin-bottom: 4px;
}

.activity-time {
  font-size: 12px;
  color: #999;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .welcome-content {
    flex-direction: column;
    text-align: center;
  }
  
  .welcome-text {
    margin-bottom: 20px;
  }
  
  .action-grid {
    grid-template-columns: 1fr;
  }
  
  .stats-row {
    margin: 0 -10px 20px -10px;
  }
  
  .stats-row .el-col {
    padding: 0 10px;
    margin-bottom: 15px;
  }
}
</style>